// Copyright (c) 2009 Hypirinha contributers
// This program is made available under the terms of the MIT License.
package org.hypirinha.html.codegen;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;

import java.util.Set;

public class AttributeFinderTest {

    @Test
    public void shouldListAttributesThatAreDirectlyReferencedInTheElementDeclaration() {
        String miniSchema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<xs:schema version=\"1.0\" xml:lang=\"en\"\n" +
                "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "  <xs:element name=\"html\">\n" +
                "    <xs:complexType>\n" +
                "      <xs:attribute name=\"id\" type=\"xs:ID\"/>\n" +
                "    </xs:complexType>\n" +
                "  </xs:element>" +
                "</xs:schema>";

        Set<String> attributes = new AttributeFinder(new SchemaParser().parse(miniSchema)).findAttributes("html");

        assertEquals(1, attributes.size());
        assertTrue(attributes.contains("id"));
    }

    @Test
    public void shouldFollowReferencesToAttributeGroups() {
        String miniSchema = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<xs:schema version=\"1.0\" xml:lang=\"en\"\n" +
                "    xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" +
                "  <xs:element name=\"html\">\n" +
                "    <xs:complexType>\n" +
                "      <xs:attributeGroup ref=\"i18n\"/>\n" +
                "      <xs:attribute name=\"id\" type=\"xs:ID\"/>\n" +
                "    </xs:complexType>\n" +
                "  </xs:element>" +
                "  <xs:attributeGroup name=\"i18n\">\n" +
                "    <xs:attribute name=\"lang\" type=\"LanguageCode\"/>\n" +
                "    <xs:attribute ref=\"xml:lang\"/>\n" +
                "  </xs:attributeGroup>" +
                "</xs:schema>";

        Set<String> attributes = new AttributeFinder(new SchemaParser().parse(miniSchema)).findAttributes("html");

        assertEquals(3, attributes.size());
        assertTrue(attributes.contains("id"));
        assertTrue(attributes.contains("lang"));
        assertTrue(attributes.contains("xml:lang"));
    }


}